<?php

/*
Plugin Name: SigmaPhp
Plugin URI:
Description: SigmaPhp utility library
Version: 1.1
Author: Peter Wraae Marino
Author URI:
*/

// example how to update plugins extenal: http://clark-technet.com/2010/12/wordpress-self-hosted-plugin-update-api

///////////////////////////////////////////////////////////////////////////////////////
// init status
///////////////////////////////////////////////////////////////////////////////////////

$spInitStatus 			= false;
$spInitScriptsStatus 	= false;
$spAdminHeadStatus		= false;

///////////////////////////////////////////////////////////////////////////////////////
// use parameters : turns on/off components in the sigmaphp
///////////////////////////////////////////////////////////////////////////////////////

$use_payment = true;

///////////////////////////////////////////////////////////////////////////////////////
// other stuff
///////////////////////////////////////////////////////////////////////////////////////

$SQL = null;

set_include_path( ABSPATH."wp-content/plugins/sigmaphp" );
require_once "html/spIncludeHtml.php";
require_once "form/spFormDatabase.php";
require_once "form/spFormButtons.php";
require_once "form/spFormBuilder.php";
require_once "spAjaxResponse.php";

if ( $use_payment )
{
	require_once "payments/spOrderDatabase.php";
	require_once "payments/spOrderFormBuilder.php";
	require_once "payments/spMerchandiseDatabase.php";
	require_once "payments/spMerchandiseFormBuilder.php";
}

require_once "form/spFormController.php";
require_once "spAbout.php";
require_once "spAjax.php";
require_once "tools/spUtil.php";
require_once "gui/spGuiListBoxNED.php";
require_once "rb.php";
require_once "sql/spSql.php";

$RM = new spRequestManager;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT!
//	you must have these actions in your own plugin. Make sure to call the spInitScripts, spAdminHead
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//add_action( 'wp_enqueue_scripts', 				'spInitScripts' );	// used for front-end
//add_action( 'admin_enqueue_scripts', 			'spInitScripts' );	// used for back-end

add_action( 'wp_head', 							'spAdminHead' );	// used for front-end
add_action( 'admin_head', 						'spAdminHead' );	// used for back-end

add_action( "init", 							"spInit" );

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

add_action( 'admin_menu', 						'spAdminMenu' );

add_action( 'wp_ajax_sp_ajax_response',			'spAjaxResponse' );		// back-end
add_action( 'wp_ajax_nopriv_sp_ajax_response',	'spAjaxResponse' );		// front-end

if ( $use_payment )
{
	register_activation_hook( 	__FILE__, 'spPluginActivated' ); 	// runs when plugin is activated 
	register_deactivation_hook( __FILE__, 'spPluginDeactivated' );	// runs on plugin deactivation
}

function spAdminHead() 
{
	// make sure we only be called once
	global $spAdminHeadStatus;
	if ( $spAdminHeadStatus )
		return;
	$spAdminHeadStatus = true;

	echo '<script type="text/javascript">';
	echo '    var spHome = "' . get_option('home') . '";';
	echo '</script>';
	
	global $use_payment;
	if ( $use_payment )
	{
		echo '<script type="text/javascript" charset="UTF-8" src="https://ssl.ditonlinebetalingssystem.dk/integration/ewindow/paymentwindow.js">';
		echo '</script>';
	}
}

function spAdminMenu()
{
	add_menu_page(
		'SigmaPhp',				// page title
		'SigmaPhp',				// menu title
		'read',					// capability
		'sp',					// menu_slug
		'',
		 plugin_dir_url( __FILE__ ).'images/sigma.gif'
	);
	
	///////////////////////////////////////
	// About
	///////////////////////////////////////
	
	$hook = add_submenu_page(
		'sp',							// parent slug
		'About',						// page title
		'About',						// menu title
		'read',							// capability
		'sp',							// menu slug
		array( new spAbout, 'render' )
	);	
	
	add_action( "admin_print_scripts-$hook", 'spInitScripts' );
}

function spInit()
{
	// make sure we only be called once
	global $spInitStatus;
	if ( $spInitStatus )
		return;
	$spInitStatus = true;

	// remove colon if there is one at the end
	$dbhost = DB_HOST;
	$dbhost = rtrim( DB_HOST, ":" );
	
	R::setup( "mysql:host=".$dbhost.";dbname=".DB_NAME, DB_USER, DB_PASSWORD );
	R::debug( false ) ;
	R::exec( "SET SESSION sql_mode=''" );

	global $SQL;
	$SQL = new spSql(
		DB_HOST,
		DB_NAME,
		DB_USER,
		DB_PASSWORD
	);
}

function spInitScripts()
{
	// make sure we only be called once
	global $spInitScriptsStatus;
	if ( $spInitScriptsStatus )
		return;
	$spInitScriptsStatus = true;

	///////////////////////////////////////////////////
	// google api
	///////////////////////////////////////////////////
	
	wp_register_script( 'jsapi', 'https://www.google.com/jsapi' );
	wp_enqueue_script( 'jsapi' );

	///////////////////////////////////////////////////
	// jquery
	///////////////////////////////////////////////////
	
	//wp_deregister_script( 'jquery' ); // this breaks jquery
	
	wp_enqueue_script( 'jquery' );
	
	wp_enqueue_style( 'jquery_ui_style', '/wp-content/plugins/sigmaphp/css/smoothness/jquery-ui-1.8.23.custom.css' );
	
	//wp_enqueue_script('jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js', array('jquery'), '1.8.23');
	wp_enqueue_script( 'jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js' );
	
	wp_enqueue_script( 'jquery-ui-core' );
	wp_enqueue_script( 'jquery-ui-mouse' );
	wp_enqueue_script( 'jquery-ui-slider' );
	wp_enqueue_script( 'jquery-ui-tabs' );
	wp_enqueue_script( 'jquery-ui-sortable' );
	wp_enqueue_script( 'jquery-ui-droppable' );
	wp_enqueue_script( 'jquery-ui-datepicker' );
	wp_enqueue_script( 'jquery-ui-dialog' );

	//////////////////////////////////////////////////////////////////////////////////////////////////////
	// jqwidgets (note need to handle commercial and non-commerical usage)
	//////////////////////////////////////////////////////////////////////////////////////////////////////

	/*
	wp_enqueue_script( 'jqwidgets_js',  		'/wp-content/plugins/sigmaphp/ext/jqwidgets-ver2.8.3/jqwidgets/jqxcore.js' );
	wp_enqueue_script( 'jqwidgets_listbox_js',  '/wp-content/plugins/sigmaphp/ext/jqwidgets-ver2.8.3/jqwidgets/jqxlistbox.js' );
	wp_enqueue_script( 'jqwidgets_buttons_js',  '/wp-content/plugins/sigmaphp/ext/jqwidgets-ver2.8.3/jqwidgets/jqxbuttons.js' );
	wp_enqueue_script( 'jqwidgets_scrollbar_js',  '/wp-content/plugins/sigmaphp/ext/jqwidgets-ver2.8.3/jqwidgets/jqxscrollbar.js' );
	
	wp_enqueue_style( 'jsqwidgets_css', '/wp-content/plugins/sigmaphp/ext/jqwidgets-ver2.8.3/jqwidgets/styles/jqx.base.css' );
	*/

	//////////////////////////////////////////////////////////////////////////////////////////////////////
	// jpicker
	//////////////////////////////////////////////////////////////////////////////////////////////////////

	wp_enqueue_script( 'jpicker_js',  '/wp-content/plugins/sigmaphp/ext/jpicker-1.1.6/jpicker-1.1.6.min.js' );
	
	wp_enqueue_style( 'jpicker_css', '/wp-content/plugins/sigmaphp/ext/jpicker-1.1.6/css/jPicker-1.1.6.min.css' );
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	// sigmaphp css
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	
	wp_enqueue_style( 'sigmaphp_css', '/wp-content/plugins/sigmaphp/html/spElement.css' );
	
	wp_enqueue_style( 'sigmaphp_main_css', '/wp-content/plugins/sigmaphp/spMain.css' );
	
	wp_enqueue_script( 'spmain_js',  '/wp-content/plugins/sigmaphp/spMain.js' );
	wp_enqueue_script( 'spmain2_js',  '/wp-content/plugins/sigmaphp/spMain2.js' );
//	wp_enqueue_script( 'spmainajax_js',  '/wp-content/plugins/sigmaphp/spMainAjax.js' );

	wp_enqueue_style( 'sigmaphp_tablesorter', '/wp-content/plugins/sigmaphp/ext/tablesorter/themes/blue/style.css' );
	wp_enqueue_script( 'spmain_tablesorter',  '/wp-content/plugins/sigmaphp/ext/tablesorter/jquery.tablesorter.min.js' );
	
	/*
	wp_enqueue_style( 'sigmaphp_tablesorter_bootstrap', '/wp-content/plugins/sigmaphp/ext/tablesorter2/docs/css/bootstrap.min.css' );
	wp_enqueue_style( 'sigmaphp_tablesorter_css', 		'/wp-content/plugins/sigmaphp/ext/tablesorter2/css/theme.bootstrap.css' );
	wp_enqueue_script( 'spmain_tablesorter',  			'/wp-content/plugins/sigmaphp/ext/tablesorter2/js/jquery.tablesorter.min.js' );
	wp_enqueue_script( 'spmain_tablesorter_widget',  	'/wp-content/plugins/sigmaphp/ext/tablesorter2/js/jquery.tablesorter.widgets.min.js' );
	wp_enqueue_script( 'spmain_tablesorter_pager',  	'/wp-content/plugins/sigmaphp/ext/tablesorter2/addons/pager/jquery.tablesorter.pager.min.js' );
	*/
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	// sigmaphp gui
	//////////////////////////////////////////////////////////////////////////////////////////////////////

	wp_enqueue_script( 'spformcontroller',	  	'/wp-content/plugins/sigmaphp/form/spFormController.js' );
	
	wp_enqueue_script( 'sppageoptions_js',	  	'/wp-content/plugins/sigmaphp/pages/spPageOptions.js' );
	
	wp_enqueue_script( 'spgui_listbox_js',	  	'/wp-content/plugins/sigmaphp/gui/spGuiListBox.js' );
	wp_enqueue_script( 'spgui_listboxned_js',	'/wp-content/plugins/sigmaphp/gui/spGuiListBoxNED.js' );
	wp_enqueue_style( 'spgui_css', 				'/wp-content/plugins/sigmaphp/gui/spGui.css' );

	//////////////////////////////////////////////////////////////////////////////////////////////////////
	// spGuiListBoxFormBinding, spDialog
	//////////////////////////////////////////////////////////////////////////////////////////////////////

	wp_enqueue_script( 'spGuiListBoxFormBinding',	  	'/wp-content/plugins/sigmaphp/gui/spGuiListBoxFormBinding.js' );
	wp_enqueue_style( 'spGuiListBoxFormBinding', 		'/wp-content/plugins/sigmaphp/gui/spGuiListBoxFormBinding.css' );
	wp_enqueue_style( 'spDialog', 		'/wp-content/plugins/sigmaphp/dialog/spDialog.css' );
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	// fpdf
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	
}

function spPluginActivated()
{
	// the epay payment system needs a page showing a receipt,.. we create this page automatically when this plugin is installed
	spCreatePage( "spPageReceipt" );
	spCreatePage( "spPaymentConfirmation" );
}

function spPluginDeactivated()
{
	spRemovePage( "spPageReceipt" );
	spRemovePage( "spPaymentConfirmation" );
}

if ( $use_payment )
{
	add_filter( 'page_template', 'wpa3396_page_template' );
	function wpa3396_page_template( $page_template )
	{
		// need to make this generic somehow.... must not call ttf stuff from here :(
		if ( is_page( 'spPageReceipt' ) ) {
			ttfInitScripts();
			$page_template = dirname( __FILE__ ) . '/testtemplate.php';
		}
		
		if ( is_page( 'spPaymentConfirmation' ) ) {
			$page_template = dirname( __FILE__ ) . '/spPaymentConfirmation.php';
		}

		return $page_template;
	}
}

/*
function my_banned_pages( $exclude_array ) {
    return array_merge( $exclude_array, array( 2853, 2857 ) ); 
}
add_filter( 'wp_list_pages_excludes', 'my_banned_pages' );
*/

?>